﻿
@{
    ViewData["Title"] = "微信消息详情";
    ViewData["container-css"] = "col-md-12";
    var list = ViewData["list"] as List<wechat_data> ?? new List<wechat_data>();
    list = list.OrderBy(x => x.CreateTime).ToList();
    var wechat = ViewData["wechatUser"] as wechat_openid ?? new wechat_openid();
    var replyList = ViewData["replyList"] as List<wechat_reply> ?? new List<wechat_reply>();
    var reply = replyList.OrderBy(x => x.create_time).LastOrDefault() ?? new wechat_reply();
    var listHistory = ViewData["listHistory"] as List<wechat_data> ?? new List<wechat_data>();
}

<div class="row">
    <div class="col-md-12">

        <ul id="tab-div" class="nav nav-tabs" role="tablist">
            <li class="active"><a href="#div-current" role="tab" data-toggle="tab">待回复消息 <i class="fa">@list.Count</i> </a></li>
            <li><a href="#div-hostory" role="tab" data-toggle="tab">7天内历史消息 <i class="fa">@listHistory.Count</i> </a></li>
        </ul>

        <div class="margin-top-10"></div>
        <div id="div-tab-list" class="tab-content">
            <div id="div-current" class="tab-pane active">
                <table class="@ViewHelper.Standard_table_css">
                    <thead>
                        <tr>
                            <th data-column-name="@nameof(wechat_data.MsgID)">消息ID</th>
                            <th data-column-name="@nameof(wechat_data.MsgType)">消息类型</th>
                            <th data-column-name="@nameof(wechat_data.Content)">文本内容</th>
                            <th data-column-name="@nameof(wechat_data.Description)">描述</th>
                            <th data-column-name="@nameof(wechat_data.FromUserName)">发送人</th>
                            <th data-column-name="@nameof(wechat_data.ToUserName)">接收人</th>
                            <th data-column-name="@nameof(wechat_data.Title)">标题</th>
                            <th data-column-name="@nameof(wechat_data.Url)">Url</th>
                            <th data-column-name="@nameof(wechat_data.CreateTimeUtc)">创建时间</th>
                        </tr>
                    </thead>
                    <tbody>
                        @{
                            if (list.Count > 0)
                            {
                                foreach (var item in list)
                                {
                                    <tr data-id="@item.MsgID">
                                        <td>@item.MsgID</td>
                                        <td>@item.MsgType</td>
                                        <td>@item.Content</td>
                                        <td>@item.Description</td>
                                        <td>@item.FromUserName</td>
                                        <td>@item.ToUserName</td>
                                        <td>@item.Title</td>
                                        <td>@item.Url</td>
                                        <td>@item.CreateTimeUtc.ToLocalDateTimeString()</td>
                                    </tr>
                                }
                            }
                            else
                            {
                                @Html.Raw(ViewHelper.Standard_no_record_tr)
                            }
                        }
                    </tbody>
                </table>

                @{
                    if (list.Count > 0)
                    {
                        <div class="row">
                            <div class="col-md-1 text-right">
                                <div>回复:</div>
                            </div>
                            <div class="col-md-11">
                                <textarea id="content" class="form-control" maxlength="1000" rows="10">@reply.content</textarea>
                                <div class="text text-info">回复的消息将在下次用户在公众号内发送消息时回传给用户, 微信编程接口不能主动推送消息, 只能被动回复</div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-offset-1 col-md-11">
                                <button class="btn btn-primary btn-xs btn-save" type="button">保存</button>
                            </div>
                        </div>
                    }
                }
            </div>

            <div id="div-hostory" class="tab-pane">
                <table class="@ViewHelper.Standard_table_css">
                    <thead>
                        <tr>
                            <th data-column-name="@nameof(wechat_data.MsgID)">消息ID</th>
                            <th data-column-name="@nameof(wechat_data.MsgType)">消息类型</th>
                            <th data-column-name="@nameof(wechat_data.Content)">文本内容</th>
                            <th data-column-name="@nameof(wechat_data.Description)">描述</th>
                            <th data-column-name="@nameof(wechat_data.FromUserName)">发送人</th>
                            <th data-column-name="@nameof(wechat_data.ToUserName)">接收人</th>
                            <th data-column-name="@nameof(wechat_data.Title)">标题</th>
                            <th data-column-name="@nameof(wechat_data.Url)">Url</th>
                            <th data-column-name="@nameof(wechat_data.CreateTimeUtc)">创建时间</th>
                        </tr>
                    </thead>
                    <tbody>
                        @{
                            if (listHistory.Count > 0)
                            {
                                foreach (var item in listHistory)
                                {
                                    <tr data-id="@item.MsgID">
                                        <td>@item.MsgID</td>
                                        <td>@item.MsgType</td>
                                        <td>@item.Content</td>
                                        <td>@item.Description</td>
                                        <td>@item.FromUserName</td>
                                        <td>@item.ToUserName</td>
                                        <td>@item.Title</td>
                                        <td>@item.Url</td>
                                        <td>@item.CreateTimeUtc.ToLocalDateTimeString()</td>
                                    </tr>
                                }
                            }
                            else
                            {
                                @Html.Raw(ViewHelper.Standard_no_record_tr)
                            }
                        }
                    </tbody>
                </table>
            </div>

        </div>

    </div>
</div>


@section scripts{
    <script type="text/javascript">
        $(".btn-save").click(function () {
            var content = $("#content").val();
            if (content.length == 0) {
                utils.ShowError("请输入回复内容");
                return;
            }

            $.ajax({
                type: "post",
                url: "@Url.Action("Save")",
                data: {
                    content: content,
                    MsgID: '@list.LastOrDefault().MsgID',
                    openid: '@wechat.openid'
                },
                error: function (request) {
                    utils.ShowError(request.data);
                },
                success: function (res) {
                    if (res.msg == 'ok') {
                        utils.ShowSuccess(res.data, function () {
                            location.reload(true);
                        });
                    }
                }
            });
        });
    </script>
}
